package com.youlai.system.controller;

import cn.hutool.core.date.DateTime;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.entity.AirRate;
import com.youlai.system.model.entity.AirTicket;
import com.youlai.system.service.AirRateService;
import com.youlai.system.service.AirTicketService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @Author Ricardo
 * @Date 2024/10/24 15:25
 * @注释
 */
@Tag(name = "14.票价信息接口")
@RestController
@RequestMapping("/api/v1/rate")
@RequiredArgsConstructor
@Slf4j
public class AirRateController {
    @Autowired
    private AirRateService airRateService;
    @Autowired
    private AirTicketService airTicketService;

    //设置航班票价并生成对应机票
    @PostMapping("/add")
    public Result addPlaneInformation(@RequestBody List<AirRate> airRates){
        if (airRateService.saveBatch(airRates)){
            /*//获取rate_id,生成票价
            List<Long> ids = airRates.stream()
                    .map(AirRate::getRateId)//提取指定属性
                    .collect(Collectors.toList());//收集到 List
            for (long id : ids) {
                for (int i = )
            }*/
            System.out.println("开始生成机票=========");
            for (AirRate a:airRates) {
                System.out.println("机票类型为："+a.getSeatType()+"==座位数为"+a.getTotal());
                for (int i = a.getTotal();i>0;i--){
                    AirTicket airTicket = new AirTicket();
                    airTicket.setFlightId(a.getFlightId());
                    airTicket.setRate(a.getTicketRate());
                    airTicket.setSeatId(i);
                    airTicket.setSeatType(a.getSeatType());
                    airTicket.setCreateTime(DateTime.now().toString());
                    log.info("生成的机票信息为",airTicket);
                    airTicketService.save(airTicket);
                }
            }
            return Result.success();
        }else {
            return Result.failed();
        }
    }

    //修改票价
    @PostMapping("/update")
    public Result updatePlaneInformation(@RequestBody List<AirRate> airRates){
            if (airRateService.saveOrUpdateBatch(airRates)){
                for (AirRate a:airRates) {
                    int i = airTicketService.updateRateById(a.getRateId(),a.getTicketRate());
                    if (i>0){
                        log.info("被修改的票数为"+i);
                    }
                }
                return Result.success();
            }else {
                return Result.failed();
            }
    }




    @PutMapping("/delete")
    public Result deletePlaneInformation(int plane_id){
        if (airRateService.removeById(plane_id)){
            return Result.success();
        }else {
            return Result.failed();
        }
    }

    //根据id查询航班信息
    @GetMapping("/select/{id}")
    public Result<AirRate> searchRateInformationById(@PathVariable long id){
        if (airRateService.getById(id) != null){
            return Result.success(airRateService.getById(id));
        }else {
            return Result.failed();
        }
    }
    @GetMapping("/select")
    public Result<List<AirRate>> searchPlaneInformation(){
        if (airRateService.list()!=null){
            return Result.success(airRateService.list());
        }else {
            return Result.failed();
        }
    }
}
